package edu.hit.pact.pgse.crawler.blog;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hit.pact.pgse.bean.RawWebPage;
import edu.hit.pact.pgse.crawler.download.FetchorFactory;
import edu.hit.pact.pgse.crawler.download.WebPageFetcher;
import edu.hit.pact.pgse.crawler.store.FileSystemWebPageStore;
import edu.hit.pact.pgse.crawler.store.WebPageStore;
import edu.hit.pact.pgse.util.Globals;

public class MainBlogFoundThread extends AbstractBlogThread {

	private int fromId;
	private int toId;
	private WebPageFetcher fetchor;
	private WebPageStore store;
	private String prefix = "http://blog.tianya.cn/blogger/blog_main.asp?BlogID=";
	private String pattern = "0000000";
	private DecimalFormat df = new DecimalFormat(pattern);

	
	public MainBlogFoundThread(String threadName, Log logger) {
		super(threadName, logger);
	}

	public MainBlogFoundThread(Log logger) {
		super(logger);
	}

	public MainBlogFoundThread(String threadName, Log logger,int fromId, int toId) {
		super(threadName, logger);
		this.fetchor = FetchorFactory.buildWebPageFetcher();
		this.store = new FileSystemWebPageStore(true);
		this.fromId = fromId;
		this.toId = toId;
	}

	@Override
	public void mainRun() {
		int index = this.fromId;
		while(index < toId){
			URL url = null;
			try {
				 url = new URL(prefix + df.format(index));
				 
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			
			RawWebPage page = this.fetchor.fetchURL(url, 2, null, null, null);
			int tryTimes = 0;
			while(page == null){
				if(tryTimes++ >= 3){
					break;
				}
				page = this.fetchor.fetchURL(url, 2, null, null, null);
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if(page == null){
				index++;
				continue;
			}
			String txt = page.getText();
			boolean flag = false;
			for(int x=1;x<14;x++){
				if(txt.equals("redirect to error.asp?msgid="+x)){
					flag = true;
					break;
				}
			}
			if(flag){
				index++;
				continue;
			}
			
			logger.info(this.getName() + " " + url.toExternalForm());
			this.store.storeWebPage(page, "test2", index+"", true);
			index++;
		}
	}
	
	public static void main(String[] args){
		Globals.readLog4jProperties();
		Log logger = LogFactory.getLog("blogId2");
		int threadNumber = 3;
		int scale =     854268;
		int startPos = 3145734;
		int eachPart = scale / threadNumber;
		
		ArrayList<AbstractBlogThread> threads = new ArrayList<AbstractBlogThread>();
		for(int i=0;i<threadNumber;++i){
			int from = startPos + i*eachPart;
			int to = from + eachPart;
			System.out.println("from = " + from + " and to = " + to);
			AbstractBlogThread thread = new MainBlogFoundThread("thread"+i,logger,from,to);
			threads.add(thread);
		}
		
		
		for(Thread thread : threads){
			thread.start();
		}
		
	}

}
